package com.animal.controller;


import com.animal.auto.JSONAuthentication;
import com.animal.entity.*;
import com.animal.mapper.AnCarMapper;
import com.animal.mapper.AnUserMapper;
import com.animal.response.Result;
import com.animal.service.AnCarService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author animal
 * @since 2022-01-29
 */
@RestController
@RequestMapping("/an-car")
public class AnCarController extends JSONAuthentication {
    @Autowired
    AnCarMapper anCarMapper;
    @Autowired
    AnCarService anCarService;
    @Autowired
    AnUserMapper anUserMapper;
    @ApiOperation(value = "添加购物车")
    @PostMapping("/add")
    public void addCar(HttpServletRequest request, HttpServletResponse response, @RequestBody AnCar anCar) throws  Exception{
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        anCar.setCaCreatTime(timestamp);
        QueryWrapper<AnCar> wrapper = new QueryWrapper<>();
        wrapper.eq("pr_id",anCar.getPrId());
       if(anCarMapper.selectOne(wrapper)!=null){
           Result error = Result.error().message("该商品购物车已存在");
           this.WriteJSON(request, response, error);
       }else{
           int i=anCarMapper.insert(anCar);
           if(i>0){
               Result ok = Result.ok();
               this.WriteJSON(request, response, ok);
           } else {
               Result error = Result.error();
               this.WriteJSON(request, response, error);
           }
       }
    }
    @ApiOperation(value = "删除购物车")
    @PostMapping("/del")
    public void delCar(HttpServletRequest request, HttpServletResponse response, @RequestParam("carId") int carId ) throws Exception{
        int i=anCarMapper.deleteById(carId);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改商品数量")
    @PostMapping("/upd")
    public void updCarNum(HttpServletRequest request, HttpServletResponse response, @RequestParam("carId") int carId,@RequestParam("prNum") int prNum)throws Exception{
        AnCar anCar1=anCarMapper.selectById(carId);
        anCar1.setPrNum(prNum);
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        anCar1.setCaUpdTime(timestamp);

        int i=anCarMapper.updateById(anCar1);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查询购物车")
    @PostMapping("/look")
    public void lookAnimal(HttpServletRequest request, HttpServletResponse response,@RequestParam("current") int current,@RequestParam("size") int size)throws  Exception{
        int total =anCarMapper.selectCount(null);
        List<AnCar> list =anCarService.findAll(current,size);
        Result ok = Result.ok().data("car",list);
        ok.data("total",total);
        this.WriteJSON(request, response, ok);
    }
    @ApiOperation(value = "通过用户id查询购物车")
    @PostMapping("/lookByUserId")
    public void lookAnimala(HttpServletRequest request, HttpServletResponse response,@RequestParam("current") int current,@RequestParam("size") int size,@RequestParam("userId") int userId)throws  Exception{
        QueryWrapper<AnCar> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        int total =anCarMapper.selectCount(wrapper);
        List<AnCar> list =anCarService.findAll(current,size);
        Result ok = Result.ok().data("car",list);
        ok.data("total",total);
        this.WriteJSON(request, response, ok);
    }
    @ApiOperation(value = "付款接口")
    @PostMapping("/fukuan")
    public void fukuan(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int userId, @RequestParam("price") float price ) throws Exception{
      AnUser anUser= anUserMapper.selectById(userId);
      float balance=anUser.getBalance();
      float yue=balance-price;
        if(yue>0){
            anUser.setBalance(yue);
           if(anUserMapper.updateById(anUser)>0) {
               Result ok = Result.ok().message("支付成功");
               this.WriteJSON(request, response, ok);
           }else{
               Result error = Result.error().message("未知错误，支付失败");
               this.WriteJSON(request, response, error);
           }
        } else {
            Result error = Result.error().message("余额不足");
            this.WriteJSON(request, response, error);
        }
    }

}

